package com.kinyx.framework.reflect.KBeanUtils_Tester;

import java.lang.reflect.Field;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.reflect.KBeanUtils;
import com.kinyx.framework.reflect.KBeanUtils.Params;

import lombok.Getter;

public class _newInstance4 {

	private static void check(final boolean isNullStr, final Object instance, final Class<? extends Object> clazz) throws NoSuchFieldException, IllegalAccessException {
		final Field field = clazz.getDeclaredField("str");
		if (!field.isAccessible()) { field.setAccessible(true); }
		final Object str = field.get(instance);
		if (isNullStr) {
			Assert.assertNull(str);
		} else {
			Assert.assertEquals("str", str);
		}
	}

	private void check(final Class<?> clazz, final boolean isNullStr, final Params<?>... params) throws NoSuchFieldException, IllegalAccessException {
		final Object instance = KBeanUtils.newInstance(clazz, params);
		Assert.assertNotNull(instance);
		check(isNullStr, instance, clazz);
	}

	@Test
	public void test() throws NoSuchFieldException, IllegalAccessException {
		this.check(Bean1.class, true, new Params<>(String.class, null));
		this.check(Bean1.class, false, new Params<>(String.class, "str"));
		this.check(Bean2.class, true, new Params<>(String.class, null), new Params<>(String.class, null));
		this.check(Bean2.class, false, new Params<>(String.class, "str"), new Params<>(String.class, null));
	}

	@Getter
	private static class Bean1 {
		private String str;
		private Bean1(final String str) { super(); this.str = str; }
	}

	@Getter
	private static class Bean2 {
		private String str;
		private String string;
		private Bean2(final String str, final String string) { super(); this.str = str; this.string = string; }
	}

}
